package io.renren.modules.hos.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.utils.ShiroUtils;
import io.renren.modules.hos.service.MedicineService;
import io.renren.modules.hos.vo.CollMedicineContentVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.hos.entity.CollMedicineEntity;
import io.renren.modules.hos.service.CollMedicineService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author ryf
 * @email 2559228480@qq.com
 * @date 2021-12-04 11:06:45
 */
@RestController
@RequestMapping("hos/collmedicine")
public class CollMedicineController {
    @Autowired
    private CollMedicineService collMedicineService;
    @Autowired
    private MedicineService medicineService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("hos:collmedicine:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = collMedicineService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("hos:collmedicine:info")
    public R info(@PathVariable("id") Long id){
		CollMedicineEntity collMedicine = collMedicineService.getById(id);

        return R.ok().put("collMedicine", collMedicine);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("hos:collmedicine:save")
    public R save(@RequestBody CollMedicineEntity collMedicine){
		collMedicineService.save(collMedicine);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("hos:collmedicine:update")
    public R update(@RequestBody CollMedicineEntity collMedicine){
		collMedicineService.updateById(collMedicine);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("hos:collmedicine:delete")
    public R delete(@RequestBody Long[] ids){
		collMedicineService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 查看收藏药品信息
     * @param medicineId
     * @return
     */
    @PostMapping("/collInfo/{id}")
    public R selectCollMedicineInfoById(@PathVariable("id") Long medicineId){
        CollMedicineContentVo collMedicineContentVo = collMedicineService.selectMedicineInfoById(medicineId);
        return R.ok().put("collMedicineVo", collMedicineContentVo);
    }

    /**
     * 添加收藏 (未完成)
     * @return
     */
    @PostMapping("/add/{id}")
    public R addCollMedicine(@PathVariable("id") Long MedicineId){
        CollMedicineEntity collMedicineEntity = new CollMedicineEntity();
        collMedicineEntity.setMedicineId(MedicineId);
        collMedicineEntity.setUserId(ShiroUtils.getHosUserId());
        collMedicineEntity.setCollDate(new Date());
        collMedicineService.save(collMedicineEntity);

        return R.ok().put("data", "收藏成功");
    }

    /**
     * 取消收藏
     */
    @RequestMapping("/cancel/{id}")
    public R cancelCollMedicine(@PathVariable("id") Long medicineId){
        collMedicineService.deleteById(medicineId);
        return R.ok().put("data", "已取消收藏");
    }


    /**
     * 查询某个用户收藏药品并分页
     * params：pageIndex，pageSize
     * @param params
     * @return
     */
    @RequestMapping("/userColl")
    public R selectUserCollMedicine(@RequestParam Map<String, Object> params){
        Long hosUserId = ShiroUtils.getHosUserId();
        PageUtils page = collMedicineService.queryListByUser(hosUserId, params);
        return R.ok().put("page", page);
    }
}
